
package com.tenentia.pcmps.swm.data.implementation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.data.DataAccessException;
import com.tenentia.pcmps.swm.data.beans.InsurancePolicyMarketingPromotion;
import com.tenentia.pcmps.swm.data.interfaces.MarketingPromotionsDAO;
import com.tenentia.pcmps.swm.data.util.IBatisUtilities;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class MarketingPromotionsDAOImpl implements MarketingPromotionsDAO {

	private static final Logger LOGGER = Logger.getLogger(MarketingPromotionsDAOImpl.class.getPackage().getName());

	MarketingPromotionsDAOImpl() {

		super();
	}

	@Override
	public void delete(InsurancePolicyMarketingPromotion promotion) throws DataAccessException {

		// TODO Auto-generated method stub

	}

	@Override
	public InsurancePolicyMarketingPromotion get(InsurancePolicyMarketingPromotion promotion) throws DataAccessException {

		try {
			promotion = (InsurancePolicyMarketingPromotion) IBatisUtilities.getSqlMapClient().queryForObject("getInsurancePolicyMarketingPromotion", promotion);
			LOGGER.debug(promotion);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return promotion;
	}

	@Override
	public List<InsurancePolicyMarketingPromotion> getAvailableMarketingPromotions(Date date) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		List<InsurancePolicyMarketingPromotion> methodResult;

		try {
			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getAvailableMarketingPromotions", date);

			if (null == tmpList || tmpList.size() == 0) {
				LOGGER.debug("No results...");
				methodResult = new ArrayList<InsurancePolicyMarketingPromotion>();
			} else {
				methodResult = new ArrayList<InsurancePolicyMarketingPromotion>();
				for (int i = 0; i < tmpList.size(); i++) {
					InsurancePolicyMarketingPromotion tmpMarketingPromotion = (InsurancePolicyMarketingPromotion) tmpList.get(i);
					methodResult.add(tmpMarketingPromotion);
					LOGGER.debug("tmpMarketingPromotion: " + tmpMarketingPromotion);
				}
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public List<InsurancePolicyMarketingPromotion> getTableItemsByRange(Object filterParameters, Object sortParameters, Integer firstRow, Integer numberOfRows) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		LOGGER.debug("firstRow: " + firstRow.intValue());
		LOGGER.debug("numberOfRows: " + numberOfRows.intValue());

		List<InsurancePolicyMarketingPromotion> methodResult;

		try {
			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getMarketingPromotionsItemsByRange", firstRow, numberOfRows);

			if (null == tmpList || tmpList.size() == 0) {
				LOGGER.debug("No results...");
				methodResult = new ArrayList<InsurancePolicyMarketingPromotion>();
			} else {
				methodResult = new ArrayList<InsurancePolicyMarketingPromotion>();
				for (int i = 0; i < tmpList.size(); i++) {
					InsurancePolicyMarketingPromotion tmpInstance = (InsurancePolicyMarketingPromotion) tmpList.get(i);
					methodResult.add(tmpInstance);
					LOGGER.debug("tmpInstance: " + tmpInstance);
				}
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public Integer getTableRowCount(Object filterParameters) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		Integer methodResult;

		try {
			methodResult = (Integer) IBatisUtilities.getSqlMapClient().queryForObject("getMarketingPromotionsRowCount");
			LOGGER.debug("methodResult: " + methodResult.intValue());
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public InsurancePolicyMarketingPromotion save(InsurancePolicyMarketingPromotion promotion) throws DataAccessException {
		
		Object methodResult = null;

		try {
			IBatisUtilities.getSqlMapClient().startTransaction();
			methodResult = IBatisUtilities.getSqlMapClient().insert("insertInsurancePolicyMarketingPromotion", promotion);
			IBatisUtilities.getSqlMapClient().commitTransaction();
		} catch (Exception e) {
			LOGGER.error(e, e);
			methodResult = e;
		} finally {
			try {
				IBatisUtilities.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				LOGGER.error(e, e);
			}
		}
		
		if(methodResult instanceof Exception) {
			throw new DataAccessException("Can't insert InsurancePolicyMarketingPromotion.");
		}

		return null;
	}

	@Override
	public InsurancePolicyMarketingPromotion update(InsurancePolicyMarketingPromotion promotion) throws DataAccessException {

		Object methodResult = null;

		try {
			IBatisUtilities.getSqlMapClient().startTransaction();
			methodResult = IBatisUtilities.getSqlMapClient().update("updateInsurancePolicyMarketingPromotion", promotion);
			IBatisUtilities.getSqlMapClient().commitTransaction();
		} catch (Exception e) {
			LOGGER.error(e, e);
			methodResult = e;
		} finally {
			try {
				IBatisUtilities.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				LOGGER.error(e, e);
			}
		}
		
		if(methodResult instanceof Exception) {
			throw new DataAccessException("Can't update InsurancePolicyMarketingPromotion.");
		}

		return null;
	}

}
